//-------------------------------inculde----------------------------------------
#include "user_interface.h"
#include "osapi.h"
#include "mem.h"
#include "espconn.h"
//------------------------------------------------------------------------------

//-----------------------------static vars--------------------------------------
static char            host[32];
static char            filename[128];
static unsigned short  port;
static struct espconn  nt_tcp_espconn;
//------------------------------------------------------------------------------

//-----------------------------static func--------------------------------------
static void http_parse_request_url(char *URL, char *host, char *filename, unsigned short *port){
  char *PA;
  char *PB;
  memset(host, 0, sizeof(host));
  memset(filename, 0, sizeof(filename));
  *port = 0;
  if(!(*URL)) return;
  PA = URL;
  if( !strncmp(PA, "http://", strlen("http://")) ){
    PA = URL + strlen("http://");
  }
  if( !strncmp(PA, "https://", strlen("https://")) ){
    PA = URL + strlen("https://");
  }
  PB = strchr(PA, '/');
  if(PB){
    memcpy(host, PA, strlen(PA) - strlen(PB));
    if(PB+1){
      memcpy(filename, PB+1, strlen(PB-1));
      filename[strlen(PB)-1] = 0;
    }
    host[strlen(PA)-strlen(PB)] = 0;
  }else{
    memcpy(host, PA, strlen(PA));
    host[strlen(PA)] = 0;
  }
  PA = strchr(host,':');
  if(PA){
    *port = atoi(PA+1);
  }else{
    *port = 80;
  }
}


static void _cb_esp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg){
  struct ip_info   this_ipfo;
  wifi_get_ip_info(STATION_IF, &this_ipfo);
  my_tcp_client_init(ipaddr, &this_ipfo.ip, port);
}
//------------------------------------------------------------------------------

//-----------------------------public func--------------------------------------
void my_http_ReadFile(char *URL){
  struct ip_addr addr;
  http_parse_request_url(URL, host, filename, &port);
  os_printf("%s\r\n", URL);
  os_printf("%s\r\n", host);
  os_printf("%s\r\n", filename);
  os_printf("%d\r\n", port);

  espconn_gethostbyname(&nt_tcp_espconn, host, &addr, _cb_esp_dns_found);
  os_printf("%d\r\n", addr.addr);
}

#if 0
//GET and POST
#define GET "GET /%s HTTP/1.1\r\nAccept: */*\r\nHost: %s\r\nConnection: Keep-Alive\r\n\r\n"
#define POST "POST /%s HTTP/1.1\r\nAccept: */*\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\nHost: %s\r\nConnection: Keep-Alive\r\n\r\n%s"
char buffer[1024];

void ICACHE_FLASH_ATTR HttpReadFile(char *URL, char *method, char *postdata){
  struct ip_addr addr;
  memset(buffer, 0, 1024);
  http_parse_request_url(URL, host, filename, &port);
  if(strcmp(method, "GET") == 0){
    os_sprintf(buffer, GET, filename, host);
  }else{
    os_sprintf(buffer, POST, filename, strlen(postdata), host, postdata);
  }
  espconn_gethostbyname(&nt_tcp_espconn, host, &addr, _cb_esp_dns_found);
}
#endif

#if 0
//cookie and flash
#define GET  "GET /%s HTTP/1.1\r\nAccept: */*\r\nHost: %s\r\nConnection: Keep-Alive\r\nCookie: %s\r\n\r\n"
#define POST "POST /%s HTTP/1.1\r\nAccept: */*\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\nHost: %s\r\nConnection: Keep-Alive\r\n\r\n%s"
char buffer[1024];

struct cookie{
  char host[32];
  char key[16];
  char value[16];
  char key1[16];
  char value1[16];
};

void HttpReadFile(char *URL, char *method, char *postdata){
  struct ip_addr addr;
  memset(buffer, 0, 1024);
  http_parse_request_url(URL, host, filename, &port);
  if(strcmp(method, "GET") == 0){
    struct cookie my_cookie;
    char tmp[128];
    memset(tmp, 0, 128);
    spi_flash_read(0x3C*4096, (uint32 *)&my_cookie, sizeof(struct cookie));
    strcat(tmp, my_cookie.key);
    strcat(tmp, "=");
    strcat(tmp, my_cookie.value);
    strcat(tmp, "; ");
    strcat(tmp, my_cookie.key1);
    strcat(tmp, "=");
    strcat(tmp, my_cookie.value1);
    os_sprintf(buffer, GET, filename, host, tmp);
  }else{
    os_sprintf(buffer, POST, filename, strlen(postdata), host, postdata);
  }
  espconn_gethostbyname(&nt_tcp_espconn, host, &addr, _cb_esp_dns_found);
}
#endif
//--------------------------------EOF-------------------------------------------



